<?php
/*
Filename	:	upload.function.php
Author		:	Jake Rodriguez Pomperada, MAED-IT
Date        :   March 19, 2011
Version		:	1.0
Description	:	Based on squarethumb.php this php file, renames them and places them in folder specified 
				by the user.
				New names of the file will be the time if has been uploaded
*/
$rename_date = date("mdys");

function createThumb($source,$dest,$size) 
{
	$thumb_size = $size;
	$size = getimagesize($source);
	$width = $size[0];
	$height = $size[1];
	if($width > $height)
	{
		$x = ceil(($width - $height) / 2 );
		$width = $height;
	} 
	elseif($height > $width)
	{
		$y = ceil(($height - $width) / 2);
		$height = $width;
	}
	$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
	$im = imagecreatefromjpeg($source);
	imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
	imagejpeg($new_im,$dest,100);
}

if($_REQUEST['Submit']):
	/*
	specify the path of the original sized images
	*/ 
	$path = "uploads/original/";
	/*
	specify the path of the resized images
	*/
	$thmb = "uploads/thumb/";
	$target_path =  $path;
	$target_path = $target_path . basename( $_FILES['_file']['name']); 
	if(move_uploaded_file($_FILES['_file']['tmp_name'], $target_path)):
		if(file_exists($path.$_FILES['_file']['name'])):
			if (exif_imagetype($path.$_FILES['_file']['name'])==IMAGETYPE_JPEG)	
			{
				if (rename($path.$_FILES['_file']['name'],$path.$rename_date.".jpeg"))
				{
					if (createThumb($path.$rename_date.".jpeg",$thmb.$rename_date.".jpeg",100)):
					header("Location: upload.function.php");
					exit();
					endif;	
				}			
			}
			else
			{
				if (unlink($path.$_FILES['_file']['name']))
				{
					header("Location: upload.function.php");
					exit();
				}
			}
		endif;
	endif;
endif;
?>
<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="_file" ><input type="submit" name="Submit" value="Upload Jpeg only">
</form>